package org.jsola.hr.helper;

import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.dto.DepInfoAddDTO;
import org.jsola.hr.dto.DepInfoUpdateDTO;
import org.jsola.hr.entity.DepInfoDO;
import org.jsola.hr.query.DepInfoQuery;
import org.jsola.hr.service.IDepInfoService;
import org.jsola.hr.vo.CompanyVO;
import org.jsola.hr.vo.DepInfoVO;
import org.jsola.module.ModuleContext;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.jsola.hr.core.AbstractTest.SITE_ID;
import static org.jsola.hr.core.TestCommonConstans.*;


/**
 * @author zhr
 */
@Component
public class DepInfoTestHelper extends BaseTestHelper {

    @Autowired
    private CompanyTestHelper companyTestHelper;
    @Autowired
    private IDepInfoService depInfoService;

    /**
     * 生成add测试数据
     * @return DepInfoAddDTO
     */
    public DepInfoAddDTO getDepInfoAddDTO(){
        DepInfoAddDTO depInfoAddDTO = new DepInfoAddDTO();
        depInfoAddDTO.setName(getRandomValue("varchar",255));
        depInfoAddDTO.setCode(getRandomValue("varchar",32));

        // 增加其他一些无关紧要的信息
        addCommonInfo(depInfoAddDTO);

        depInfoAddDTO.setCompanyId(companyTestHelper.saveCompanyExample().getId());
        depInfoAddDTO.setLevel(SafeKit.getInteger(getRandomValue("int",10)));
        depInfoAddDTO.setSort(SafeKit.getInteger(getRandomValue("int",10)));
        depInfoAddDTO.setParentId(getRandomValue("varchar",32));
        return depInfoAddDTO;
    }

    /**
     * 增加其他一些无关紧要的信息
     * @param depInfoAddDTO 部门新增实体
     */
    private void addCommonInfo(DepInfoAddDTO depInfoAddDTO) {
        depInfoAddDTO.setType(SafeKit.getInteger(getRandomValue("tinyint",4)));
        depInfoAddDTO.setVirural(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        depInfoAddDTO.setLeader(getRandomValue("varchar",32));
        depInfoAddDTO.setAddressId(SafeKit.getInteger(getRandomValue("int",11)));
        depInfoAddDTO.setAddress(getRandomValue("varchar",255));
        depInfoAddDTO.setDesc(getRandomValue("varchar",2000));
        depInfoAddDTO.setFormalNum(SafeKit.getInteger(getRandomValue("int",11)));
        depInfoAddDTO.setInformalNum(SafeKit.getInteger(getRandomValue("int",11)));
    }


    /**
     * 生成update测试数据
     * @return DepInfoUpdateDTO
     */
    public DepInfoUpdateDTO getDepInfoUpdateDTO(DepInfoVO depInfoVO){
        return depInfoService.selectDOById(depInfoVO.getId(), SITE_ID).to(DepInfoUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return DepInfoQuery
     */
    public DepInfoQuery getDepInfoQuery() {
        DepInfoQuery depInfoQuery = new DepInfoQuery();
        List<DepInfoDO> depInfoDOList = depInfoService.list(new DepInfoDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(depInfoDOList)) {
            DepInfoDO depInfoDO = depInfoDOList.get(0);
            depInfoQuery = depInfoDO.to(DepInfoQuery.class);
            if (depInfoDO.getGmtCreate() != null) {
                depInfoQuery.setGmtCreateStart(DateKit.getBeforeDay(depInfoDO.getGmtCreate(),-1));
                depInfoQuery.setGmtCreateEnd(DateKit.getBeforeDay(depInfoDO.getGmtCreate(),1));
            }
            if (depInfoDO.getGmtModified() != null) {
                depInfoQuery.setGmtModifiedStart(DateKit.getBeforeDay(depInfoDO.getGmtModified(),-1));
                depInfoQuery.setGmtModifiedEnd(DateKit.getBeforeDay(depInfoDO.getGmtModified(),1));
            }
        }
        return depInfoQuery;
    }

    /**
     * 保存示例
     * @return DepInfoVO
     */
    public DepInfoVO saveDepInfoExample(){
        DepInfoAddDTO addDTO = this.getDepInfoAddDTO();
        DepInfoVO depInfoVO = depInfoService.save(addDTO,buildTokenUser());
        return depInfoVO;
    }


    /**
     * 批量创建部门
     * @param moduleContext 上下文,参数见TestCommonConstans
     */
    public void batchSaveDepInfoExample(ModuleContext moduleContext) {
        // 初始化一级site部门
        initSiteDeptInfo(moduleContext);
        // 公司
        CompanyVO companyVO = companyTestHelper.saveCompanyExample();
        // 上下文增加公司
        moduleContext.add(CODE_COMPANY, companyVO);
        // 公司部门
        DepInfoVO companyDepInfo = depInfoService.selectByCode(companyVO.getId(), SITE_ID, companyVO.getId());
        // 上下文增加公司
        moduleContext.add(CODE_DEP_COMPANY, companyDepInfo);

        List<DepInfoVO> depInfoList = new ArrayList<>();
        // 公司下增加部门,结构如下
        //    1-小桥
        //      2-风控
        //      2-京普(3-开发,3-设计)
        //    1-实验室(无人员)
        DepInfoAddDTO addDTO1_1 = getDepInfoAddDTOByInfo(
                "小桥",
                "1-小桥",
                companyDepInfo.getLevel(),
                companyDepInfo.getId(),
                companyVO.getId());
        DepInfoVO depInfoVO1_1 = depInfoService.save(addDTO1_1, buildTokenUser());
        depInfoList.add(depInfoVO1_1);
        if (!Objects.isNull(depInfoVO1_1)) {
            // 2-风控
            DepInfoAddDTO addDTO2_1 = getDepInfoAddDTOByInfo(
                    "风控",
                    "2-风控",
                    depInfoVO1_1.getLevel(),
                    depInfoVO1_1.getId(),
                    companyVO.getId());
            DepInfoVO depInfoVO2_1 = depInfoService.save(addDTO2_1, buildTokenUser());
            depInfoList.add(depInfoVO2_1);
            // 2-京普
            DepInfoAddDTO addDTO2_2 = getDepInfoAddDTOByInfo(
                    "京普",
                    "2-京普",
                    depInfoVO1_1.getLevel(),
                    depInfoVO1_1.getId(),
                    companyVO.getId());
            DepInfoVO depInfoVO2_2 = depInfoService.save(addDTO2_2, buildTokenUser());
            depInfoList.add(depInfoVO2_2);
            if (!Objects.isNull(depInfoVO2_2)) {
                // 3-开发
                DepInfoAddDTO addDTO3_1 = getDepInfoAddDTOByInfo(
                        "开发",
                        "3-开发",
                        depInfoVO2_2.getLevel(),
                        depInfoVO2_2.getId(),
                        companyVO.getId());
                DepInfoVO depInfoVO3_1 = depInfoService.save(addDTO3_1, buildTokenUser());
                depInfoList.add(depInfoVO3_1);
                // 3-设计
                DepInfoAddDTO addDTO3_2 = getDepInfoAddDTOByInfo(
                        "设计",
                        "3-设计",
                        depInfoVO2_2.getLevel(),
                        depInfoVO2_2.getId(),
                        companyVO.getId());
                DepInfoVO depInfoVO3_2 = depInfoService.save(addDTO3_2, buildTokenUser());
                depInfoList.add(depInfoVO3_2);
            }
        }
        // 1-实验室(无人员)
        DepInfoAddDTO addDTO1_2 = getDepInfoAddDTOByInfo(
                "实验室",
                "1-实验室(无人员)",
                companyDepInfo.getLevel(),
                companyDepInfo.getId(),
                companyVO.getId());
        DepInfoVO depInfoVO1_2 = depInfoService.save(addDTO1_2, buildTokenUser());
        depInfoList.add(depInfoVO1_2);
        // 上下文增加公司
        moduleContext.add(CODE_DEP_LIST, depInfoList);
    }

    /**
     * 根据固定信息构建部门
     *
     * @param name        部门名称
     * @param code        部门code
     * @param parentLevel 父层级
     * @param parentId    父级id
     * @param companyId   公司id
     * @return 部门新增实体
     */
    private DepInfoAddDTO getDepInfoAddDTOByInfo(String name, String code, int parentLevel, String parentId, String companyId) {
        DepInfoAddDTO depInfoAddDTO = new DepInfoAddDTO();
        depInfoAddDTO.setName(name);
        depInfoAddDTO.setCode(code);

        // 增加其他一些无关紧要的信息
        addCommonInfo(depInfoAddDTO);

        depInfoAddDTO.setCompanyId(companyId);
        depInfoAddDTO.setLevel(parentLevel + 1);
        depInfoAddDTO.setSort(SafeKit.getInteger(getRandomValue("int", 10)));
        depInfoAddDTO.setParentId(parentId);
        return depInfoAddDTO;
    }


    /**
     * 初始化一级site部门
     *
     * @param moduleContext 上下文
     */
    private void initSiteDeptInfo(ModuleContext moduleContext) {
        DepInfoAddDTO depInfoAddDTO = new DepInfoAddDTO();
        depInfoAddDTO.setName("site");
        depInfoAddDTO.setParentId("0");
        depInfoAddDTO.setLevel(1);
        depInfoAddDTO.setCode(SITE_ID);
        depInfoAddDTO.setVirural(false);
        depInfoAddDTO.setType(0);
        depInfoAddDTO.setSort(100);
        DepInfoVO depInfoVO = depInfoService.save(depInfoAddDTO, buildTokenUser());
        // 上下文增加公司
        moduleContext.add(CODE_DEP_SITE, depInfoVO);
    }


}

